clear      
clear mata
clear matrix
version 11.0                                                        
set more off
set memory 1000m
capture log close
set scheme s2color
# delimit ; 
*==================================================================================================; 
* File-Name:       	IDACC_replicationFile.do                                 		
* Date:            	February 27, 2014 
* Author:          	DD                                       
* Purpose:         	Analysis, tables, and figures for IDACC paper
* Data Used:       	LandaDuell_data.dta
*==================================================================================================; 
* Variables:
* treatment: 1 Baseline, 2 Identity,
* ingroup: 0 Out-group, 1 In-group, 2 Baseline
* electiondecision: 0 not retained, 1 retained
* choice 1: 5-20 investment into effort pre-election, acronym: c1
* choice 2: 5-20 investment into effort post-election, acronym: c2
* type: 20-50 level of competence, acronym: tv
* noise: -15 - +15 random draw from uniform distribution
* type_omega: approx. 5-65, acronmym: extv
* period: 1-20 round of play
* firsthalf: 0 Second half, 1 First half of session
*
* Generated by this do-file:
* bias: -1 Out-group bias, 1 In-group bias
*==================================================================================================;
*==================================================================================================; 
* I. Define macros and programs
*==================================================================================================; 
* set path to subdirectories;
local data "data";
local output "figures";
local programs "programs";

* define locals;
local graphr "graphr(fc(white) lc(white) ifc(white) ilc(white))";
local grcom "ycommon xcommon imargin(small)";
local stdl "lp(solid) lc(black) lw(thick)";
local stdl_spike "lp(solid) lc(gs12) lw(thick)";
local pointer "msi(zero) mcol(white) mlabp(0) mlabc(black) mlabs(medlarge)";
local area = 976;

* define programs;
program drop _all; 
program diff_means, rclass; version 11;
sum variable if(class_variable==1&included==1); local val1 = r(mean);
sum variable if(class_variable==0&included==1); local val2 = r(mean);
return scalar diff = `val2' - `val1'; end;

program diff_roc, rclass; version 11;
roctab electiondecision variable if(ingroup==2); local baseline = r(area);
roctab electiondecision variable if(ingroup==1); local ingroup = r(area);
roctab electiondecision variable if(ingroup==0); local outgroup = r(area);
return scalar test1 = (`baseline' - `ingroup');
return scalar test2 = (`baseline' - `outgroup');
return scalar test3 = (`ingroup' - `outgroup'); end;

program modelfit, rclass; version 11;
predict p if e(sample); sum p; local n = r(N); 
g phat1 = sum(p) if(electiondecision==1);
g phat0 = sum(1-p) if(electiondecision==0);
sum phat1; local sumphat1 = r(max); sum phat0; local sumphat0 = r(max);
count if(electiondecision==1); local pmc = r(N)/`n'; 
count if((electiondecision==1&p>.5)|(electiondecision==0&p<.5));
local pcp = r(N)/`n';
return scalar pcp = `pcp'; local epcp = (`sumphat1' + `sumphat0')/`n';
return scalar epcp = `epcp'; return scalar pmc = `pmc';
return scalar pre = (`pcp' - `pmc')/(1 - `pmc');	
drop p phat1 phat0; end;

program diffroc_bias, rclass; version 11;
roctab electiondecision variable if(bias==1) ; local inbias = r(area);
roctab electiondecision variable if(bias==-1); local outbias = r(area);
return scalar diff = (`inbias' - `outbias'); end;

*==================================================================================================;
*==================================================================================================; 
* II. Analysis  
*==================================================================================================; 
* 5. Aggregate patterns
*==================================================================================================; 
* 5.1 Retention decision (electiondecision)
*==================================================================================================; 
* Figure 1: Effect of type_omega and/or choice 1 on electiondecision
*==================================================================================================; 
* Load data;
use `data'/LandaDuell_data, clear; 

keep if(((treatment==baseline|treatment==identity)&period>0)&playerid==voter);  

******************************************************************************;
* Generate qoi;
g lowc1 = 0;  replace lowc1 = 1 if(c1<13); g x = .; g mean = .; g lb = .; g ub = .; 
local k = 1;
foreach i in 2 1 0 {;
	foreach j in 1 0 {;
		bs qoi = (r(mu_1)-r(mu_2)), reps(100) seed(01010) cl(session) l(90):
		ttest election if(ingroup==`i'&lowc1==`j'), by(exlowtype); 
		mat ci = e(ci_percentile); replace lb = ci[1,1] in `k'; replace ub = ci[2,1] in `k'; 
		mat qoi = e(b); replace mean = qoi[1,1] in `k'; replace x = `k' in `k'; local k = `k' + 1;
		};
	local k = `k' + 1;	
	};
local k = 9;
foreach i in 2 1 0 {;
	foreach j in 1 0 {;
		bs qoi = (r(mu_1)-r(mu_2)), reps(100) seed(01010) cl(session) l(90):
		ttest election if(ingroup==`i'&exlowtype==`j'), by(lowc1); 
		mat ci = e(ci_percentile); replace lb = ci[1,1] in `k'; replace ub = ci[2,1] in `k'; 
		mat qoi = e(b); replace mean = qoi[1,1] in `k'; replace x = `k' in `k';
		local k = `k' + 1;
		};
	local k = `k' + 1;
	};

******************************************************************************;
* Plot qoi;
gr tw 
	(bar mean x if(x==1|x==2), col(gray) barw(.9))
	(bar mean x if(x==4|x==5), col(black) barw(.9))
	(bar mean x if(x==7|x==8), lc(black) fc(white) lw(medium) barw(.9))
	(rcap lb ub x if(x<=8), lc(gs12) lw(thick)),
	ysc(range(-.05 .6)) ylab(0(.1).6) xsc(off) leg(off) `graphr' yti(" ") 
	yline(0, lc(black) lp(solid)); 
gr export `output'/bar_retention_bytypec1.pdf, replace;
gr export `output'/bar_retention_bytypec1.eps, replace; 

g inlowtype = ingroup*exlowtype;
reg electiondecision ingroup exlowtype inlowtype if(ingroup~=0), cl(session) l(90);
reg electiondecision ingroup exlowtype inlowtype if(ingroup~=1), cl(session) l(90);

gr tw 
	(bar mean x if(x==9|x==10), col(gray) barw(.9))
	(bar mean x if(x==12|x==13), col(black) barw(.9))
	(bar mean x if(x==15|x==16), lc(black) fc(white) lw(medium) barw(.9))
	(rcap lb ub x if(x>=9&x<=16), lc(gs12) lw(thick)),
	ysc(alt range(-.05 .6)) ylab(0(.1).6) xsc(off) leg(off) `graphr' yti(" ")
	yline(0, lc(black) lp(solid)); 
gr export `output'/bar_retention_byc1type.pdf, replace;
gr export `output'/bar_retention_byc1type.eps, replace;

g inlowc1 = ingroup*lowc1; g inlowtypelowc1 = ingroup*exlowtype*lowc1;
reg electiondecision ingroup exlowtype lowc1 inlowtype inlowc1 inlowtypelowc1 if(ingroup~=0), 
	cl(session) l(90);
reg electiondecision ingroup exlowtype lowc1 inlowtype inlowc1 inlowtypelowc1 if(ingroup~=1), 
	cl(session) l(90);

******************************************************************************;
* Diff in- vs out-group in effect of choice 1 significant?;
program drop _all; 
program diff, rclass; version 11;
sum election if(ingroup==1&lowc1==1); local inlow = r(mean);
sum election if(ingroup==1&lowc1==0); local inhigh = r(mean);
sum election if(ingroup==0&lowc1==1); local outlow = r(mean);
sum election if(ingroup==0&lowc1==0); local outhigh = r(mean);
return scalar diff = (`inlow' - `inhigh') - (`outlow' - `outhigh'); end; 
bs diff = r(diff), reps(100) seed(01010) l(90): diff;
mat ci = e(ci_percentile); replace lb = ci[1,1]; replace ub = ci[2,1]; 
mat diff = e(b); replace mean = diff[1,1];
di "-------------------------------------------------";
di "Difference in effect on choice 1 between in-group and out-group matches: " diff[1,1];
di "Lower Bound: "ci[1,1];
di "Upper Bound: "ci[2,1]; 
di "-------------------------------------------------";

*==================================================================================================; 
* 5.2 Investment into effort pre- (choice 1) and post-election (choice 2)
*=================================================================================================;
* Summary statistics of choice 1 and choice 2
*=================================================================================================;
* Load data;
use `data'/LandaDuell_data, clear; 
keep if(((treatment==baseline|treatment==identity)&period>0)&playerid==representative);

******************************************************************************;
* Generate qoi;
forval i = 0/2 {;
	di "-------------------------------------------------";
	di "Ingroup: `i'";
	sum c1 if(ingroup==`i');
	sum c2 if(ingroup==`i'&electiondecision==1);
	qui unique sessub if(ingroup==`i'); local N = r(sum);
	qui bysort sessub: egen mean_c2 = mean(c2) if(electiondecision==1);
	qui unique sessub if(ingroup==`i'&mean_c2==5); 
	local p = r(sum)/`N'; drop mean_c2;
	di "-------------------------------------------------";
	di "Proportion of subjects who always choose 5 in choice 2: `p'";
	di "-------------------------------------------------";
	};

*=================================================================================================; 
* Figure 2: Expected values of choice 1 over type and ingroup
*=================================================================================================;
* Load data;
use `data'/LandaDuell_data, clear; 
keep if(((treatment==baseline|treatment==identity)&period>0)&playerid==representative);

******************************************************************************;
* Generate qoi;
g y = .;  g lb = .; g ub = .;
forval i = 0/2 {;
	estsimp reg c1 tv if(ingroup==`i');
	forval j = 20/50 {;
		setx tv `j'; 
		simqi, genev(y0);
		sum y0; local mean = r(mean);
		replace y = `mean' if(ingroup==`i'&tv==`j');
		drop y0;
	};
	drop b*;
};
	
g tvpl = tv+.25;
g tvplpl = tv+.5;

******************************************************************************;
* Plot qoi;
gr tw 
(lowess c1 tv if(ingroup==1), lc(black) lw(vthick))
(lowess c1 tv if(ingroup==2), lc(gray) lw(vthick))
(lowess c1 tv if(ingroup==0), lc(black) lw(vthick) lp(dash))
(scatter y tv if(ingroup==1), mc(black) msiz(large))
(scatter y tv if(ingroup==2), mc(gray) msiz(large))
(scatter y tv if(ingroup==0), mlc(black) mfc(white) msiz(large)),
`graphr' leg(off) ysc(range(10 15)) ylab(10(1)15) yti("") xti("")
ysize(7);
gr export `output'/scatter_ev_c1.eps, replace; 
gr export `output'/scatter_ev_c1.pdf, replace;

*=================================================================================================;
* Test of difference in distribution of choice 1 over treatment and ingroup
*=================================================================================================;
* Load data;
use `data'/LandaDuell_data, clear; 
keep if(((treatment==baseline|treatment==identity)&period>0)&playerid==representative);

******************************************************************************;
* Generate qoi; 
foreach l in 1 0 {; 
	foreach i in 0 1 2 {; 
		preserve; 
		keep if(ingroup!=`i'&lowtype==`l'); 
		di "-------------------------------------------------";
		di "without ingroup = `i' for lowtypes: `l'";
		ranksum c1, by(ingroup); 
		fprank c1, by(ingroup);
		bs var = r(Var), reps(100) seed(01010) 
		saving(var_c1`i'_`l'): sum c1, d;
		permute c1 z = r(z), reps(100): ranksum c1, 
		by(ingroup);
		di "-------------------------------------------------";
		restore; 
		}; 
	};

use var_c10_1, clear; g g = 0;
append using var_c11_1; replace g = 1 if(g==.); 
append using var_c12_1; replace g = 2 if(g==.); 
ttest var if(g~=2), by(g); ttest var if(g~=1), by(g); ttest var if(g~=0), by(g);

use var_c10_0, clear; g g = 0;
append using var_c11_0; replace g = 1 if(g==.); 
append using var_c12_0; replace g = 2 if(g==.); 
ttest var if(g~=2), by(g); ttest var if(g~=1), by(g); ttest var if(g~=0), by(g);

erase var_c10_1.dta;
erase var_c10_0.dta; 
erase var_c11_1.dta;
erase var_c11_0.dta;
erase var_c12_1.dta;
erase var_c12_0.dta; 

*=================================================================================================;
* 6. Individual-level strategies of play
*=================================================================================================;
*  6.1 Identifying identity-dependent individual-level behavior
*=================================================================================================;
* 6.1.1 measuring group biases in retention decisions
*=================================================================================================;
* Load data; 
use `data'/LandaDuell_data, clear; 
keep if(treatment==identity&period>0&playerid==voter);

******************************************************************************;
* Generate qoi;
replace extv = round(extv);
forval in = 0/1 {;
	g rarea`in' = .; g norarea`in' = .; g meanarea`in' = .; g overlap`in' = .;
	if(`in'==0){; local set "alt"; };
	else {; local set ""; };
	vallist sessub; 
	foreach s in `r(list)' {;
		preserve; keep if(sessub==`s'&ingroup==`in');
		local obs = 61*16; set obs `obs'; egen extvval = seq(), from(5) to(65); 
		bysort extvval: egen c1val = seq(), from(5) to(20);  
		forval r = 0/1 {;
			if(`r'==0){; local v "nor"; local sign1 "<="; local sign2 ">="; };
			else {; local v "r"; local sign1 ">="; local sign2 "<="; };
			g `v'extvindex = .; g `v'index = 0;
			local i = 1; gsort -extv; vallist extv if(election==`r');  
			foreach extv in `r(list)' {;
				g `v'c1index`i' = .;
				vallist c1 if(extv==`extv'&election==`r');
				g `v'extv`i' = `extv'; 
				local j = 1;
				foreach c1 in `r(list)' {; 
					g `v'c1`i'_`j' = `c1'; 
					local j = `j'+1;
					};
				replace `v'c1index`i' = `j'-1;
				local i = `i'+1;
				};
			replace `v'extvindex = `i'-1;
			sum `v'extvindex; local max = r(max); 
			forval i = 1/`max' {;
				sum `v'c1index`i'; 
				forval j = 1/`r(max)' {; 
				if(`r'==0){; local extvmax = 65; local c1max = 20; };
				else {; local extvmax = 0; local c1max = 0; };
				sum `v'extv`i'; sum `v'c1`i'_`j'; sum extvval; 
				replace `v'index = 1 
				if(extvval`sign1'`v'extv`i'&c1val`sign1'`v'c1`i'_`j'&
				(`extvmax'`sign2'`v'extv`i'|`c1max'`sign2'`v'c1`i'_`j'));
				sum extvval if(`v'index==1); local extvmax = r(max); 
				sum c1val if(`v'index==1); local c1max = r(max);
				};
			};
		};
		g y = .; g ymaxnor = .; g yminr = .; g xmaxnor = .; g xminr = .;
		g x = extvval; g nory = .; g ry = .;
		forval extv = 5(1)65 {;
			sum c1val if(extvval==`extv'&norindex==1);
			replace nory = r(max) if(extvval==`extv');
			sum c1val if(extvval==`extv'&rindex==1);
			replace ry = r(min) if(extvval==`extv');
			};
		forval c1 = 5(1)20 {;
			sum extvval if(c1val==`c1'&norindex==1);
			replace nory = `c1' if(extvval==r(max)&c1val==`c1');
			sum extvval if(c1val==`c1'&rindex==1);
			replace ry = `c1' if(extvval==r(min)&c1val==`c1');
			};	
		count if(rindex==1); local rarea = r(N); 
		count if(norindex==1); local norarea = r(N);
		count if(rindex==1&norindex==1); local overlap = r(N);

*=================================================================================================;
* Appendix Figure D.1: Examples of Actually observed and assumed (type_omega, choice 1)-
* pairs by electiondecision for four subjects
*=================================================================================================;
		if(r(N)==0){; local scatter " "; };
		else if(r(N)>0){; local scatter 
			"(scatter c1val extvval if(rindex==1&norindex==1), mc(white)
			msiz(huge) ms(S))
			(scatter c1val extvval if(rindex==1&norindex==1), mc(black))"; };
		gr tw
		(scatter c1val extvval if(rindex==1), mc(gs14) msi(huge) ms(S))
		(scatter c1val extvval if(norindex==1), mc(gs8) msiz(huge) ms(S))
		`scatter'
		(scatter nory x, mc(black) msiz(huge) ms(S))
		(scatter ry x, mc(black) msiz(huge) ms(S))
		(scatter c1 extv if(election==1), mfc(gs14) mlc(black) msiz(huge)
		mlw(thick))
		(scatter c1 extv if(election==0), mfc(gs8) mlc(black) msiz(huge)
		mlw(thick)),
		`graphr' leg(off) ysc(`set' range(5 20)) ylab(5(5)20) 
		xsc(range(5 65)) xlab(5(10)65) xti("") yti(""); 
		gr export `output'/scatter_bias_`s'_`in'.pdf, replace; 
		gr export `output'/scatter_bias_`s'_`in'.eps, replace;
		restore; 
		replace rarea`in' = `rarea' if(sessub==`s'); 
		replace norarea`in' = `norarea' if(sessub==`s'); 	
		replace overlap`in' = `overlap' if(sessub==`s');
		};
	};

g area = `area'; 
g propoverlap1 = overlap1/area;
g propoverlap0 = overlap0/area;

replace meanarea1 = (area - (norarea1+rarea1))/2 if(overlap1==0);
replace meanarea0 = (area - (norarea0+rarea0))/2 if(overlap0==0);
replace meanarea1 = (area - (norarea1+rarea1) + overlap1)/2 if(overlap1~=0);
replace meanarea0 = (area - (norarea0+rarea0) + overlap0)/2 if(overlap0~=0);
g area1 = area-(norarea1+meanarea1);
g area0 = area-(norarea0+meanarea0);
g diffarea = area1 - area0;
g lbarea = norarea1 - norarea0;
g ubarea = rarea1 - rarea0;
g bias = 1 if(diffarea>0); replace bias = -1 if(diffarea<0);
g lbbias = 1 if(lbarea>0); replace lbbias = -1 if(lbarea<0);
g ubbias = 1 if(ubarea>0); replace ubbias = -1 if(ubarea<0);

g propbias = abs(diffarea/area);
g absbias = .;
local j = 1;
forval i = 0(.01).56 {;
	replace absbias = `j' if(propbias>`i'&propbias<=`i'+.01);
	local j = `j' + 1;
}; 

save `data'/LandaDuell_data_bias, 
replace;

*=================================================================================================;
* Distribution of bias and monotonicity measure 
* Load data; 
use `data'/LandaDuell_data_bias.dta, clear; 

******************************************************************************;
* Generate qoi;
unique sessub if(bias==-1); 
unique sessub if(bias==1);
qui unique sessub if(overlap0==0); local n0 = r(sum)/70;
qui unique sessub if(overlap1==0); local n1 = r(sum)/70;
di "-------------------------------------------------";
di "Proportion of subjects in identity treatment with no overlap of retention";
di "and non-retention set:";
di "In-group: `n1'";
di "Out-group: `n0'";
di "-------------------------------------------------";
qui unique sessub if(overlap0/area>.1); local n0 = r(sum)/70;
qui unique sessub if(overlap1/area>.1); local n1 = r(sum)/70;
di "-------------------------------------------------";
di "Proportion of subjects in identity treatment with larger overlap of retention";
di "and non-retention set larger than 10% of the (type_omega,choice 1)-space:";
di "In-group: `n1'";
di "Out-group: `n0'";
di "-------------------------------------------------";

*=================================================================================================;
* 6.1.2 What drives the behavioral group biases?
*=================================================================================================;
* Manifestations of behavioral bias
*=================================================================================================;
* Figure 3: Proportion retained for in-group and out-group biased voters
*=================================================================================================;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to subjects who are representatives in the first
* half of the session; 
vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	};

use `data'/LandaDuell_data.dta, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==voter);

g bias = .; vallist sessub if(treatment==identity); 
foreach s in `r(list)' {;
	replace bias = `bias`s'' if(sessub==`s');
	};

g inbias = 0 if(bias==-1); replace inbias = 1 if(bias==1); replace inbias = 2 if(bias==.); 
******************************************************************************;
* Generate qoi;
g lowc1 = 0; replace lowc1 = 1 if(c1<13); g x = .; g mean = .; g lb = .; g ub = .; local m = 1;
foreach i in 2 1 0 {; 
	if(`i'==2){;
	foreach j in 1 0 {; 
		foreach k in 1 0 {; 
				bs qoi = r(mean), reps(100) seed(01010) l(90) cl(session): sum election 
					if(inbias==`i'&exlowtype==`j'&lowc1==`k'); 
				mat ci = e(ci_percentile); replace lb = ci[1,1] in `m'; replace ub = ci[2,1] in `m'; 
				mat qoi = e(b); replace mean = qoi[1,1] in `m'; replace x = `m'  in `m'; 
				local m = `m' + 1;
				}; 
			};
		};
	if(`i'<2){;
		foreach j in 1 0 {; 
			foreach k in 1 0 {;
				foreach l in 1 0 {;
					bs qoi = r(mean), reps(100) seed(01010) l(90) cl(session): sum election 
						if(inbias==`i'&ingroup==`j'&exlowtype==`k'&lowc1==`l'); 
					mat ci = e(ci_percentile); replace lb = ci[1,1] in `m'; replace ub = ci[2,1] in `m'; 
					mat qoi = e(b); replace mean = qoi[1,1] in `m'; replace x = `m'  in `m'; 
					local m = `m' + 1;
					}; 
				};
			};
		};	
	};
	
replace x = x+1 if(x>=3); replace x = x+3 if(x>=6); 
replace x = x+1 if(x>=11);  replace x = x+2 if(x>=14);
replace x = x+1 if(x>=18);  replace x = x+3 if(x>=21);
replace x = x+1 if(x>=26);  replace x = x+2 if(x>=29); 
replace x = x+1 if(x>=33);  

gr tw 
	(bar mean x if(x==1|x==2|x==9|x==10|x==16|x==17|x==24|x==25|x==31|x==32), 
		lc(black) fc(white) lw(thick) barw(.9) yaxis(1 2))
	(bar mean x if(x==4|x==5|x==12|x==13|x==19|x==20|x==27|x==28|x==34|x==35), 
		lc(black) fc(gs4) lw(thick) barw(.9))
	(rcap lb ub x, lc(gs12) lw(thick)),
	ylab(0(.2)1) xlab(1(5)35) xsc(off) leg(off) `graphr' yti("") 
	name(baseline, replace) xsiz(10);

gr export `output'/sum_stats_electiondection_bias.eps, replace; 	
gr export `output'/sum_stats_electiondection_bias.pdf, replace; 	

*=================================================================================================;
* Motivational rationals for behavioral bias
*=================================================================================================;
* In-bias a function of favorable retention choices experienced as low type in in-matches?
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to subjects who are representatives in the first
* half of the session; 
vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	sum diffarea if(sessub==`s'); local diff`s' = r(mean);
	sum absbias if(sessub==`s'); local absbias`s' = r(mean);
	};

use `data'/LandaDuell_data.dta, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative&firsthalf==1);

g bias = .; g diffarea = .; g absbias = .; vallist sessub if(treatment==identity); 
foreach s in `r(list)' {;
	local r = `s' + 10000;
	replace bias = `bias`r'' if(sessub==`s');
	replace diffarea = `diff`r'' if(sessub==`s');
	replace absbias = `absbias`r'' if(sessub==`s'); 
	};

******************************************************************************;
* Generate qoi;
* Define being treated favorably as being retained as low type;
g favorable = .; g prop_favorable = .;
replace favorable = 1 if(election==1&ingroup==1&lowtype==1);
bysort sessub: egen total_favorable = count(favorable);

vallist sessub;
foreach s in `r(list)' {;
	count if(ingroup==1&lowtype==1&sessub==`s'); 
	replace prop_favorable = total_favorable/r(N) if(sessub==`s');
	};	

* Run difference-in-means and difference-in-distribution by whether agent was being
* treated favorably or unfavorably more than half of the time;
g class_favorable = 0 if(prop_favorable<.5); 
replace class_favorable = 1 if(prop_favorable>=.5);
g variable = diffarea/((65-5)*(20-5)); g class_variable = class_favorable; 
g included = 1 if(bias==1&prop_favorable~=.);
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90) cl(sessub absbias): 
diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum variable if(included==1), by(class_variable);
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);	

di "-------------------------------------------------";
di "Difference in bias between agents who are treated favorably and those who are not: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

*=============================================================================;
* High and type-independent choice 1 of in-group biased voters;  
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to subjects when in the role of representatives; 
vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	sum diffarea if(sessub==`s'); local diff`s' = r(mean);
	sum absbias if(sessub==`s'); local absbias`s' = r(mean);
	};

use `data'/LandaDuell_data.dta, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative);

g bias = .; g diffarea = .; g absbias = .; vallist sessub if(treatment==identity); 
foreach s in `r(list)' {;
	local r = `s' + 10000;
	replace bias = `bias`r'' if(sessub==`s');
	replace diffarea = `diff`r'' if(sessub==`s');
	replace absbias = `absbias`r'' if(sessub==`s'); 
	};

******************************************************************************;
* Generate qoi;
g variable = c1; g class_variable = lowtype; g included = 1 if(bias==1&ingroup==1);
sum c1 if(included==1); local c1 = r(mean); local sd = r(sd);
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90) cl(sessub absbias): 
diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum c1 if(included==1), by(lowtype);
fprank c1 if(included==1), by(lowtype);
ttest c1 if(included==1), by(lowtype);
di "-------------------------------------------------";
di "Average choice 1: `c1', st.dev: `sd'"; 
di "Difference in choice 1 between low and high types in in-group matches for in-biased voters: " 
	qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

*=============================================================================;
* Exit survey;  
* Load data;  
use `data'/LandaDuell_data_bias, clear; 
keep if(session~=33);

******************************************************************************;
* Generate qoi;
g inbias = 0; replace inbias = 1 if(bias==1);
merge m:1 sessub using `data'/LandaDuell_exitSurvey;
tab inbias q4; *race 
ranksum q1, by(inbias);

* Question to voters:
* Please tell us how your decisions as a voter were affected by being matched with a representative 
* who was a fan of the same artist (that is, if you were a ``Klee'' and the representative was a 
* ``Klee'', or if you were a ``Kandinsky'' and the representative was a ``Kandinsky'') rather than 
* with a representative who was a fan of a different artist. 
* (1) I was generally more likely to re-elect him/her;
*(2) I was more likely to re-elect such representative only if s/he had what seemed like a high 
* true number, and otherwise it did not matter;
* (3) I was more likely to re-elect such representative only if s/he had what seemed like a low 
* true number, and otherwise it did not matter;
* (4) Whether the representative I was matched with was a fan of the same artist did not matter for 
* my choices as a voter;
replace idbehaviorasv = 1 if(idbehaviorasv==12); * Subject asnwered 1 and 2;
replace idbehaviorasv = 4 if(idbehaviorasv==24); * Subject asnwered 2 and 4;
replace idbehaviorasv = 4 if(idbehaviorasv==34); * Subject asnwered 3 and 4;
replace idbehaviorasv = . if(idbehaviorasv==99); * Subject gave nonsensical answer;
g x = .; g y = .;
foreach i in 1 0 {;
	g freqvoter`i' = .; g freqrep`i' = .; 
	unique sessub if(inbias==`i'); local N = r(sum);
	vallist idbehaviorasvoter if(inbias==`i'); 
	foreach j in `r(list)' {;
		unique sessub if(idbehaviorasvoter==`j'&inbias==`i');
		replace freqvoter`i' = r(sum)/`N' in `j'; replace x = `j' in `j'; 
		};
	vallist idbehaviorasrep if(inbias==`i'); 
	foreach j in `r(list)' {;
		unique sessub if(idbehaviorasrep==`j'&inbias==`i');
		replace freqrep`i' = r(sum)/`N' in `j'; replace y = `j' in `j'; 
		};		 
	};

unique sessub if(inbias==1); local N1 = r(sum); 
unique sessub if(inbias==0); local N0 = r(sum); 
unique sessub if(inbias==1&idbehaviorasv==1); local n1 = 100*r(sum)/`N1';
unique sessub if(inbias==0&idbehaviorasv==1); local n0 = 100*r(sum)/`N0';
di "-------------------------------------------------";
di "More likely to retain in-group representative:";
di "`n1'% of in-biased voters";
di "`n0'% of out-biased voters";
di "-------------------------------------------------";
unique sessub if(inbias==1&idbehaviorasv==4); local n1 = 100*r(sum)/`N1';
unique sessub if(inbias==0&idbehaviorasv==4); local n0 = 100*r(sum)/`N0';
di "-------------------------------------------------";
di "Group ID did not matter:";
di "`n1'% of in-biased voters";
di "`n0'% of out-biased voters";
di "-------------------------------------------------";

* Question to representatives:
* Please tell us how your decisions as a representative were affected by being matched 
* with a voter who was a fan of the same artist (that is, if you were a Klee and the 
* voter was a Klee, or if you were a Kandinsky and the voter was a Kandinsky) rather 
* than with a voter who was a fan of a different artist. Please choose all that apply:
* (1) It made my group choice 1 higher when my true number was relatively low;
* (2) It made my group choice 1 higher when my true number was relatively high;
* (3) It made my group choice 1 higher regardless of whether my true number was relatively low or relatively high;
* (4) It made my group choice 1 lower when my true number was relatively low;
* (5) It made my group choice 1 lower when my true number was relatively high;
* (6) It made my group choice 1 higher regardless of whether my true number was relatively low or relatively high;
* Collect same answers when subjects gave multiple answers;
* (7) ID did not matter	
replace y = 3 if(y==5); replace y = 4 if(y==7);
replace y = 5 if(y==13); replace y = 6 if(y==15); replace y = 7 if(y==24);
replace y = 8 if(y==145); replace y = 9 if(y==99);
unique sessub if(inbias==1); local N1 = r(sum); 
unique sessub if(inbias==0); local N0 = r(sum); 
unique sessub if(inbias==1&idbehaviorasr==7); local n1 = 100*r(sum)/`N1';
unique sessub if(inbias==0&idbehaviorasr==7); local n0 = 100*r(sum)/`N0';
di "-------------------------------------------------";
di "Group ID did not matter:";
di "`n1'% of in-biased voters as representatives";
di "`n0'% of out-biased voters as representatives";
di "-------------------------------------------------";

*=============================================================================;
* Summary stats of choice 1 and choice 2 of in-group bias votes when in the role of a representative;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to subjects when in the role of representatives; 
vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	sum diffarea if(sessub==`s'); local diff`s' = r(mean);
	sum absbias if(sessub==`s'); local absbias`s' = r(mean);
	};

use `data'/LandaDuell_data.dta, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative);

g bias = .; g diffarea = .; g absbias = .; vallist sessub if(treatment==identity); 
foreach s in `r(list)' {;
	local r = `s' + 10000;
	replace bias = `bias`r'' if(sessub==`s');
	replace diffarea = `diff`r'' if(sessub==`s');
	replace absbias = `absbias`r'' if(sessub==`s'); 
	};

******************************************************************************;
* Generate qoi;
sum c1 if(bias==1);
di "-------------------------------------------------";
di "Average of choice 1 of in-group biased voters in the role of agents: " 
	r(mean);
di	"St.dev.: " r(sd);
di "-------------------------------------------------";
sum c1 if(bias==-1);
di "-------------------------------------------------";
di "Average of choice 1 of out-group biased voters in the role of agents: " 
	r(mean);
di	"St.dev.: " r(sd);
di "-------------------------------------------------"; 
sum c2 if(bias==1&c2>0);
di "-------------------------------------------------";
di "Average of choice 2 of in-group biased voters in the role of agents: " 
	r(mean);
di	"St.dev.: " r(sd);
di "-------------------------------------------------";
sum c2 if(bias==-1&c2>0);
di "-------------------------------------------------";
di "Average of choice 2 of out-group biased voters in the role of agents: " 
	r(mean);
di	"St.dev.: " r(sd);
di "-------------------------------------------------";

bysort sessub: egen mean_c1_in = mean(c1) if(ingroup==1&bias==1);
bysort sessub: egen mean_c1_out = mean(c1) if(ingroup==0&bias==1);
bysort sessub: egen mean_c2_in = mean(c2) if(ingroup==1&c2>0&bias==1);
bysort sessub: egen mean_c2_out = mean(c2) if(ingroup==0&c2>0&bias==1);
di "-------------------------------------------------";
di "In-group";
pwcorr mean_c1_in mean_c2_in, sig;
di "Out-group";
pwcorr mean_c1_out mean_c2_out, sig;
di "-------------------------------------------------";

* Same for high types;
drop mean_*;
bysort sessub: egen mean_c1_in = mean(c1) if(ingroup==1&bias==1&lowtype==0);
bysort sessub: egen mean_c1_out = mean(c1) if(ingroup==0&bias==1&lowtype==0);
bysort sessub: egen mean_c2_in = mean(c2) if(ingroup==1&c2>0&bias==1&lowtype==0);
bysort sessub: egen mean_c2_out = mean(c2) if(ingroup==0&c2>0&bias==1&lowtype==0);
di "-------------------------------------------------";
di "In-group";
pwcorr mean_c1_in mean_c2_in, sig;
di "Out-group";
pwcorr mean_c1_out mean_c2_out, sig;
di "-------------------------------------------------";

*=================================================================================================;
* Out-group biased voters: Experience as favorable retention choices as low type in out-group 
* matches when in the role of a representative;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to subjects when in the role of representatives; 
vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	sum diffarea if(sessub==`s'); local diff`s' = r(mean);
	sum absbias if(sessub==`s'); local absbias`s' = r(mean);
	};

use `data'/LandaDuell_data.dta, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative);

g bias = .; g diffarea = .; g absbias = .; vallist sessub if(treatment==identity); 
foreach s in `r(list)' {;
	local r = `s' + 10000;
	replace bias = `bias`r'' if(sessub==`s');
	replace diffarea = `diff`r'' if(sessub==`s');
	replace absbias = `absbias`r'' if(sessub==`s'); 
	};

******************************************************************************;
* Generate qoi;
* Differences in bias;
preserve;
keep if(bias==-1);
g retained_as_lowtype = 0 if(lowtype==1&ingroup==0); 
replace retained_as_lowtype = 1 if(lowtype==1&ingroup==0&electiondecision==1);

ttest diffarea, by(retained_as_lowtype); local diff = abs((r(mu_2)-r(mu_1))/r(mu_2));
di "-------------------------------------------------";
di "Decrease in in-bias for out-group biased voters when retained as low types in out-group matches";
di "in contrast to those low types in out-group matches who are not retained:";
di "`diff'% of in-bias";
di "p: " r(p_l);  
di "-------------------------------------------------";

* Same for low type_omega;
g retained_as_exlowtype = 0 if(exlowtype==1&ingroup==0); 
replace retained_as_exlowtype = 1 if(exlowtype==1&ingroup==0&electiondecision==1);

ttest diffarea, by(retained_as_exlowtype); local diff = abs((r(mu_2)-r(mu_1))/r(mu_2));
di "-------------------------------------------------";
di "Decrease in in-bias for out-group biased voters when retained as low type_omega in out-group matches";
di "in contrast to those low type_omega in out-group matches who are not retained:";
di "`diff'% of in-bias";
di "p: " r(p_l);  
di "-------------------------------------------------";
restore; 

* What determines choice 2 once out-group biased voters are in the role of representatives?;
preserve;
keep if(bias==-1&firsthalf==1);
g lowtype_in = 1 if(lowtype==1&ingroup==1); g lowtype_out = 1 if(lowtype==1&ingroup==0);
g retained_as_lowtype_in = .; g retained_as_lowtype_out = .;
replace retained_as_lowtype_in = 1 if(lowtype==1&ingroup==1&electiondecision==1); 
replace retained_as_lowtype_out = 1 if(lowtype==1&ingroup==0&electiondecision==1); 
bysort sessub: egen sessub_count_lowtype_in = count(lowtype_in);
bysort sessub: egen sessub_count_lowtype_out = count(lowtype_out);
bysort sessub: egen retained_in_count = count(retained_as_lowtype_in);
bysort sessub: egen retained_out_count = count(retained_as_lowtype_out);
g prop_retained_in = retained_in_count/sessub_count_lowtype_in;
g prop_retained_out = retained_out_count/sessub_count_lowtype_out;

g absbias_sessub = absbias*sessub; keep if(c2>0);
reg c2 c.c1 c.tv ingroup c.prop_retained_in c.prop_retained_out 
	c.c1#c.extv c.c1#ingroup c.c1#c.prop_retained_in c.c1#c.prop_retained_out
	c.extv#ingroup c.extv#c.prop_retained_in c.extv#c.prop_retained_out
	ingroup#c.prop_retained_in c.ingroup#c.prop_retained_out
	c.extv#c.c1#ingroup c.c1#c.extv#c.prop_retained_in c.c1#c.extv#c.prop_retained_out, 
	vce(cl absbias_sessub);
foreach i in c1 extv 1.ingroup prop_retained_in prop_retained_out {;
	di "-------------------------------------------------";
	di "Marginal effect of `i':";
	margins, dydx(`i') vce(unconditional); mat b = r(b);
	di "Percentage of choice 2-space: " abs(b[1,1]/15)*100; 
	di "-------------------------------------------------";
};
restore;

preserve;
keep if(bias==1&firsthalf==1);
g lowtype_in = 1 if(lowtype==1&ingroup==1); g lowtype_out = 1 if(lowtype==1&ingroup==0);
g retained_as_lowtype_in = .; g retained_as_lowtype_out = .;
replace retained_as_lowtype_in = 1 if(lowtype==1&ingroup==1&electiondecision==1); 
replace retained_as_lowtype_out = 1 if(lowtype==1&ingroup==0&electiondecision==1); 
bysort sessub: egen sessub_count_lowtype_in = count(lowtype_in);
bysort sessub: egen sessub_count_lowtype_out = count(lowtype_out);
bysort sessub: egen retained_in_count = count(retained_as_lowtype_in);
bysort sessub: egen retained_out_count = count(retained_as_lowtype_out);
g prop_retained_in = retained_in_count/sessub_count_lowtype_in;
g prop_retained_out = retained_out_count/sessub_count_lowtype_out;

g absbias_sessub = absbias*sessub; keep if(c2>0);
reg c2 c.c1 c.tv ingroup c.prop_retained_in c.prop_retained_out 
	c.c1#c.extv c.c1#ingroup c.c1#c.prop_retained_in c.c1#c.prop_retained_out
	c.extv#ingroup c.extv#c.prop_retained_in c.extv#c.prop_retained_out
	ingroup#c.prop_retained_in c.ingroup#c.prop_retained_out
	c.extv#c.c1#ingroup c.c1#c.extv#c.prop_retained_in c.c1#c.extv#c.prop_retained_out, 
	vce(cl absbias_sessub);
foreach i in c1 extv 1.ingroup prop_retained_in prop_retained_out {;
	di "-------------------------------------------------";
	di "Marginal effect of `i':";
	margins, dydx(`i') vce(unconditional); mat b = r(b);
	di "Percentage of choice 2-space: " abs(b[1,1]/15)*100; 
	di "-------------------------------------------------";
};
restore;		

*=============================================================================;
* Difference in choice 2 between in- and out-group biased voters
* Summary stats of choice 1 and choice 2 of in-group bias votes when in the role of a representative;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to subjects when in the role of representatives; 
vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	sum diffarea if(sessub==`s'); local diff`s' = r(mean);
	sum absbias if(sessub==`s'); local absbias`s' = r(mean);
	};

use `data'/LandaDuell_data.dta, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative);

g bias = .; g diffarea = .; g absbias = .; vallist sessub if(treatment==identity); 
foreach s in `r(list)' {;
	local r = `s' + 10000;
	replace bias = `bias`r'' if(sessub==`s');
	replace diffarea = `diff`r'' if(sessub==`s');
	replace absbias = `absbias`r'' if(sessub==`s'); 
	};

******************************************************************************;
* Generate qoi;
g inbias = 0 if(bias==-1); replace inbias = 1 if(bias==1);

preserve;
g variable = c2; g class_variable = inbias; g included = 1 if(c2>0);
sum c2 if(included==1); local c1 = r(mean); local sd = r(sd);
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90) cl(sessub absbias): 
diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum c2 if(included==1), by(inbias);
fprank c2 if(included==1), by(inbias);
ttest c2 if(included==1), by(inbias);
di "-------------------------------------------------";
di "Difference in choice 2 between in- and out-group biased voters: " 
	qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

* Same varying ingroup and lowtype;
preserve;
g variable = c2; g class_variable = inbias; g included = 1 if(c2>0&ingroup==1);
sum c2 if(included==1); local c1 = r(mean); local sd = r(sd);
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90) cl(sessub absbias): 
diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum variable if(included==1), by(class_variable);
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in choice 2 between in- and out-group biased voters in in-group matches: " 
	qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = c2; g class_variable = inbias; g included = 1 if(c2>0&ingroup==0);
sum c2 if(included==1); local c1 = r(mean); local sd = r(sd);
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90) cl(sessub absbias): 
diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum variable if(included==1), by(class_variable);
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in choice 2 between in- and out-group biased voters in out-group matches: " 
	qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = c2; g class_variable = inbias; g included = 1 if(c2>0&lowtype==1);
sum c2 if(included==1); local c1 = r(mean); local sd = r(sd);
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90) cl(sessub absbias): 
diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum c2 if(included==1), by(inbias);
fprank c2 if(included==1), by(inbias);
ttest c2 if(included==1), by(inbias);
di "-------------------------------------------------";
di "Difference in choice 2 between in- and out-group biased voters in out-group matches: " 
	qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = c2; g class_variable = inbias; g included = 1 if(c2>0&lowtype==0);
sum c2 if(included==1); local c1 = r(mean); local sd = r(sd);
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90) cl(sessub absbias): 
diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum variable if(included==1), by(class_variable);
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in choice 2 between in- and out-group biased voters in out-group matches: " 
	qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

*=============================================================================;
* Difference in correctness of guesses in painter quiz between in- and out-group biased voters
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to original dataset with painting quiz stage; 
vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean); 
	};

use `data'/LandaDuell_data, clear; 
keep if(treatment==identity&period==-1); 

g bias = .; vallist sessub; 
foreach s in `r(list)' {;
	sum playerid if(sessub==`s');
	if(r(mean)==1){;
		local r = `s'+10000;
		replace bias = `bias`r'' if(sessub==`s');
		};
	else {;	
		replace bias = `bias`s'' if(sessub==`s');
		};
	};

******************************************************************************;
* Generate qoi;
g inbias = 0 if(bias==-1); replace inbias = 1 if(bias==1);
g rightdecision1 = 0; replace rightdecision1 = 1 if(paintingdecision1==2);
g rightdecision2 = 0; replace rightdecision2 = 1 if(paintingdecision2==1);
g rightdecision3 = 0; replace rightdecision3 = 1 if(paintingdecision3==1);

g prop_correct = (rightdecision1 + rightdecision2 + rightdecision3)/3;

g variable = prop_correct; g class_variable = inbias; g included = 1;
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90): 
diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum variable if(included==1), by(class_variable);
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in # of correct painter quiz guesses between in- and out-group biased voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

*=============================================================================;
* Epistemic group experience;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to original dataset with painting quiz stage; 
vallist sessub; 
foreach s in `r(list)' {;
	sum diffarea if(sessub==`s'); local diffarea`s' = r(mean); 
	};

use `data'/LandaDuell_data, clear; 
keep if(treatment==identity&period==-1); 

g diffarea = .; vallist sessub; 
foreach s in `r(list)' {;
	sum playerid if(sessub==`s');
	if(r(mean)==1){;
		local r = `s'+10000;
		replace diffarea = `diffarea`r'' if(sessub==`s');
		};
	else {;	
		replace diffarea = `diffarea`s'' if(sessub==`s');
		};
	};

forval i = 1/3 {;
	g decision`i' = paintingdecision`i';
	};
	
g wrongmajdecision1 = 1; g wrongmajdecision2 = 2; g wrongmajdecision3 = 2;
g rightdecision1 = 0; replace rightdecision1 = 1 if(decision1==2);
g rightdecision2 = 0; replace rightdecision2 = 1 if(decision2==1);
g rightdecision3 = 0; replace rightdecision3 = 1 if(decision3==1);
g rightdecision = rightdecision1 + rightdecision2 + rightdecision3;

g againstmaj = 0 if(wrongmajdecision1==decision1&wrongmajdecision2==decision2&
wrongmajdecision3==decision3);
replace againstmaj = 1 if(
	(wrongmajdecision1~=decision1&wrongmajdecision2==decision2&wrongmajdecision3==decision3)|
	(wrongmajdecision1==decision1&wrongmajdecision2~=decision2&wrongmajdecision3==decision3)|
	(wrongmajdecision1==decision1&wrongmajdecision2==decision2&wrongmajdecision3~=decision3));
replace againstmaj = 2 if(
	(wrongmajdecision1~=decision1&wrongmajdecision2~=decision2&wrongmajdecision3==decision3)|
	(wrongmajdecision1~=decision1&wrongmajdecision2==decision2&wrongmajdecision3~=decision3)|
	(wrongmajdecision1==decision1&wrongmajdecision2~=decision2&wrongmajdecision3~=decision3));
replace againstmaj = 3 if(
	wrongmajdecision1~=decision1&decision1~=decision2&wrongmajdecision3~=decision3);

g againstdummy = againstmaj; replace againstdummy = 1 if(againstmaj>0);

******************************************************************************;
* Generate qoi;
g variable = diffarea; g class_variable = againstdummy; g included = 1;
bs diff = r(diff) if(included==1), reps(100) seed(01010) l(90): diff_means; 
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
ranksum variable if(included==1), by(class_variable);
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in D_i by group experience: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

*==================================================================================================;
* 6.3 Evolution of play
*==================================================================================================;
* Biases by halves of session
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

******************************************************************************;
* Generate qoi; 
unique sessub if(bias==1&firsthalf==1); * In-group biased voters in first half of session;
unique sessub if(bias==-1&firsthalf==1); * Out-group biased voters in first half of session;
unique sessub if(bias==1&firsthalf==0); * In-group biased voters in second half of session;
unique sessub if(bias==-1&firsthalf==0); * Out-group biased voters in second half of session;

g inbias = 0; replace inbias = 1 if(bias==1);
g variable = diffarea; g class_variable = firsthalf; g included = 1;
bs diff = r(diff), reps(100) seed(01010) l(90) cl(sessub absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable, by(class_variable);
fprank variable, by(class_variable);
ttest variable, by(class_variable);
di "-------------------------------------------------";
di "Difference in D_i by halves of session: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

*=============================================================================;
* Retention rate by biases in first and second half of each session
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

******************************************************************************;
* Generate qoi;
preserve;
g variable = election; g class_variable = ingroup; g included = 1 if(bias==1&firsthalf==1);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(sessub absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
prtest variable if(included==1), by(class_variable); 
di "-------------------------------------------------";
di "Difference in election decision between in- and out-group matches for in-group biased voters";
di "in first half of session: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = election; g class_variable = ingroup; g included = 1 if(bias==1&firsthalf==0);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(sessub absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
prtest variable if(included==1), by(class_variable); 
di "-------------------------------------------------";
di "Difference in election decision between in- and out-group matche for in-group biased voters";
di "in second half of session: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = election; g class_variable = ingroup; g included = 1 if(bias==-1&firsthalf==1);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(sessub absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
prtest variable if(included==1), by(class_variable); 
di "-------------------------------------------------";
di "Difference in election decision between in- and out-group matche for out-group biased voters";
di "in first half of session: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = election; g class_variable = ingroup; g included = 1 if(bias==-1&firsthalf==0);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(sessub absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1];
prtest variable if(included==1), by(class_variable); 
di "-------------------------------------------------";
di "Difference in election decision between in- and out-group matches for out-group biased voters";
di "in second half of session: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

*==================================================================================================;
* Appendix: Robustness Checks
*==================================================================================================;
* A. Summary statistics
*==================================================================================================;
* Table A.1 Number of subjects, Klees, and Kandinskys by session
*==================================================================================================;
* Load data; 
use `data'/LandaDuell_data, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative);

* Baseline;
unique sessub if(session==7);
unique sessub if(session==8);
unique sessub if(session==10);

* Identity;
unique sessub if(session==33); 
unique sessub if(session==33&grouptype==klee); 
unique sessub if(session==33&grouptype==kandinsky); 
unique sessub if(session==34);
unique sessub if(session==34&grouptype==klee); 
unique sessub if(session==34&grouptype==kandinsky); 
unique sessub if(session==35);
unique sessub if(session==35&grouptype==klee); 
unique sessub if(session==35&grouptype==kandinsky); 
unique sessub if(session==36); 
unique sessub if(session==36&grouptype==klee); 
unique sessub if(session==36&grouptype==kandinsky); 

*==================================================================================================;
* Table A.2 Summary statistics of main variables by treatment
*==================================================================================================;
* Baseline;
sum electiondecision tv extv c1 if(treatment==baseline);
sum electiondecision c2 if(treatment==baseline&c2>0);

* Identity
sum electiondecision tv extv c1 if(treatment==identity);
sum electiondecision tv extv c1 if(treatment==identity&ingroup==1);
sum electiondecision tv extv c1 if(treatment==identity&ingroup==0);
sum c2 if(treatment==identity&c2>0);
sum c2 if(treatment==identity&ingroup==1&c2>0);
sum c2 if(treatment==identity&ingroup==0&c2>0);

*==================================================================================================;
* Robustness of the choice 1 - model
*==================================================================================================;
* Table B: Regression of choice 1 on type and treatment dummies
*==================================================================================================;
* Load data;
use `data'/LandaDuell_data, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative); 

******************************************************************************;
* Generate qoi;	
g outgroup = 0; replace outgroup = 1 if(ingroup==0);
replace baseline = 0 if(baseline==.); 
g retention = election;

forval i = 2/10 {;
	g round`i' = 0; replace round`i' = 1 if(round==`i');
};

xtset sessub round;
foreach v in tv baseline outgroup retention tempprofit {;
	g l`v' = l.`v'; 
	};

* Models;
di "=====================================================";
di "Model 1:";
reg c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup;
mat V = e(V); di "ingroup:" %6.3f _b[tv] "," %6.3f sqrt(V[1,1]);
margins, dydx(tv) subpop(baseline) post; mat V = e(V); mat b = e(b);
di "baseline:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]); 
reg c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup;
qui margins, dydx(tv) subpop(outgroup) post; mat V = e(V); mat b = e(b);
di "outgroup:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);

di "=====================================================";
di "Model 2:";
reg c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup, 
vce(cl sessub);
mat V = e(V); di "ingroup:" %6.3f _b[tv] "," %6.3f sqrt(V[1,1]);
margins, dydx(tv) subpop(baseline) vce(unconditional) post; mat V = e(V); 
mat b = e(b); di "baseline:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]); 
reg c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup, 
vce(cl sessub);
margins, dydx(tv) subpop(outgroup) vce(unconditional) post; mat V = e(V); 
mat b = e(b); di "outgroup:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);

di "=====================================================";
di "Model 3:";
tobit c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup, 
ll(5) ul(20) vce(cl sessub);
mat V = e(V); di "ingroup:" %6.3f _b[tv] "," %6.3f sqrt(V[1,1]);
margins, dydx(tv) subpop(baseline) vce(unconditional) post; mat V = e(V); 
mat b = e(b); di "baseline:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]); 
tobit c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup, 
ll(5) ul(20) vce(cl sessub);
margins, dydx(tv) subpop(outgroup) vce(unconditional) post; mat V = e(V); 
mat b = e(b); di "outgroup:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);

di "=====================================================";
di "Model 4:";
tobit c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup i.round, 
ll(5) ul(20) vce(cl sessub);
mat V = e(V); di "ingroup:" %6.3f _b[tv] "," %6.3f sqrt(V[1,1]);
margins, dydx(tv) subpop(baseline) vce(unconditional) post; mat V = e(V); 
mat b = e(b); di "baseline:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]); 
tobit c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup i.round, 
ll(5) ul(20) vce(cl sessub);
qui margins, dydx(tv) subpop(outgroup) vce(unconditional) post; mat V = e(V); 
mat b = e(b); di "outgroup:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);

di "=====================================================";
di "Model 5:";
xtreg c1 tv i.round if(ingroup==1), fe vce(cl sessub); mat b = e(b);
mat V = e(V); di "ingroup:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);
xtreg c1 tv i.round if(baseline==1), fe vce(cl sessub); mat b = e(b);
mat V = e(V); di "baseline:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);
xtreg c1 tv i.round if(outgroup==1), fe vce(cl sessub); mat b = e(b);
mat V = e(V); di "outgroup:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);

di "=====================================================";
di "Model 6:";
xtreg c.c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup i.round, 
re vce(robust);
mat V = e(V); di "ingroup:" %6.3f _b[tv] "," %6.3f sqrt(V[1,1]); 
margins, dydx(tv) subpop(baseline) post; mat V = e(V); 
mat b = e(b); di "baseline:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]); 
xtreg c1 tv baseline outgroup c.tv#i.baseline c.tv#i.outgroup i.round, 
re vce(robust);
margins, dydx(tv) subpop(outgroup) post; mat V = e(V); 
mat b = e(b); di "outgroup:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);

* Test model 5 vs 6;
forval i = 0/2 {;
	xtreg c1 tv round* if(ingroup==`i'), re; 
	scalar theta = e(thta_50); global list c1 tv round*; sort sessub;  
	foreach x of varlist $list {; 
		qui by sessub: egen mean`x' = mean(`x');
		qui g md`x' = `x' - mean`x';
		qui g red`x' = `x' - theta*mean`x';
		};	
	reg redc1 redtv redround* mdtv mdround*; 
	test mdtv mdround1 mdround2 mdround3 mdround4 mdround5 mdround6
	mdround7 mdround8 mdround9 mdround10;
	drop md* red* mean*;
	};
	
di "=====================================================";
di "Model 7:";
xttobit c1 tv baseline outgroup c.tv#i.baseline c.tv#i.outgroup i.round, 
ll(5) ul(20) vce(boot); 
mat V = e(V); di "ingroup:" %6.3f _b[tv] "," %6.3f sqrt(V[1,1]); 
margins, dydx(tv) subpop(baseline) post; mat V = e(V);  
mat b = e(b); di "baseline:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]); 
xttobit c1 tv baseline outgroup c.tv#i.baseline c.tv#i.outgroup i.round, 
ll(5) ul(20) vce(boot);
margins, dydx(tv) subpop(outgroup) post; mat V = e(V); 
mat b = e(b); di "outgroup:" %6.3f b[1,1] "," %6.3f sqrt(V[1,1]);

di "=====================================================";
di "Model 8:";
forval i = 1/2 {;
		xtabond c1 tv round* if(ingroup==`i'), twostep lag(1) maxldep(1) 
	maxlags(2) artest(3) pre(ltv lbaseline loutgroup)
	endogenous(lretention ltempprofit);
	estat sargan;
	xtabond c1 tv round* if(ingroup==`i'), twostep lag(1) maxldep(1) 
	maxlags(2) artest(3) vce(robust) pre(ltv lbaseline loutgroup)
	endogenous(lretention ltempprofit);
	estat abond; 
	di "ingroup cat:`i' " %6.3f _b[tv], %6.3f _se[tv]; 
	};

xtabond c1 tv round* if(ingroup==0), twostep lag(1) maxldep(1) 
maxlags(2) artest(3) endogenous(lretention ltempprofit);
estat sargan;
xtabond c1 tv round* if(ingroup==0), twostep lag(1) maxldep(1) 
maxlags(2) artest(3) vce(robust) endogenous(lretention ltempprofit);
estat abond;
di "ingroup cat:0 " %6.3f _b[tv], %6.3f _se[tv]; 

*==================================================================================================;
* D. Robustness of average treatment effects
*==================================================================================================;
* D.1 Retention decision 
*==================================================================================================;
use `data'/LandaDuell_data, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==2); 
g extvc1 = extv*c1;

******************************************************************************;
* Generate and plot qoi;
foreach variable in "extv" "extvc1" {;
	* ROC-curve retention decision over type;
	qui {; roccomp electiondecision `variable', g by(ingroup) `graphr' `set' leg(off)
	plot1opts(lc(black) lp(solid) lw(thick) mfc(white) mlc(black)) 
	plot2opts(lc(black) lp(solid) lw(thick) mc(black)) 
	plot3opts(lc(gs12) lp(solid) lw(thick) mc(gs12))
	rlopts(lc(black) lw(thick) lp(dash)) yti("") xti("");  
	gr export `output'/roc_`variable'.pdf, replace;
	gr export `output'/roc_`variable'.eps, replace; };


	* Test of difference in area under the ROC-curve retention decision over type;
	preserve;
	qui {; g variable = `variable'; g qoi = .; g lb = .; g ub = .; g p = .;
	roccomp electiondecision variable if(ingroup!=0), by(ingroup); local p1 = r(p); 
	roccomp electiondecision variable if(ingroup!=1), by(ingroup); local p2 = r(p);
	roccomp electiondecision variable if(ingroup!=2), by(ingroup); local p3 = r(p); 
	bs test1 = r(test1) test2 = r(test2) test3 = r(test3), reps(100) l(90) seed(01010) cl(session): 
		diff_roc; 
	mat diff = e(b); mat ci = e(ci_percentile); 
	forval i = 1/3 {; 
		replace lb = ci[1,`i'] in `i'; replace ub = ci[2,`i'] in `i'; replace qoi = diff[1,`i'] in `i';
		replace p = `p`i'' in `i';
		}; 

	mkmat qoi lb ub p if(qoi~=.), mat(results); mat rown results = 
	"Baseline_-_In-Group" "Baseline_-_Out-Group" "In-Group_-_Out-Group";
	mat coln results = "Diff" "Lower_Bound" "Upper_Bound" "p-Value"; 
	restore; };
	di "-------------------------------------------------";
	di "electiondecision vs `variable'";
	di "Difference in area under ROC-curve: Baseline vs In-group, baseline vs out-group,";
	di "in-group vs out-group";
	mat list results;
	di "-------------------------------------------------";
	};

*==================================================================================================;
* E. Examples and robustness of bias measure
*==================================================================================================;
* E.1 See Section 6.1.1 above for example figures
*==================================================================================================;
* E.2 Robustness of bias measure
*==================================================================================================;
* Generate bias measure based on difference in areas with retention probabilities from the baseline
* Load data;
use `data'/LandaDuell_data, clear; 
keep if(treatment==baseline&playerid==voter);

******************************************************************************;
* Generate qoi;
replace extv = round(extv); 
forval i = 2/10 {;
	g round`i' = 0; replace round`i' = 1 if(round==`i');
};

g extvc1 = extv*c1;
probit election extv c1 extvc1; 
lroc; modelfit; di "`r(pcp)', `r(epcp)', `r(pre)'"; 
predict p if e(sample);
forval i = 5/65 {;
	forval j = 5/20 {;
		local p`i'_`j' = normal(_b[_cons] + _b[extv]*`i' + _b[c1]*`j' + _b[extvc1]*`i'*`j');
		};
	};

use `data'/LandaDuell_data, clear; 
keep if(treatment==identity&period>0&playerid==voter);
g p = .; g retention = .; replace extv = round(extv);
forval i = 5/65 {;
	forval j = 5/20 {;
		replace p = `p`i'_`j'' if(extv==`i'&c1==`j');	
		};
	};

forval in = 0/1 {;
	g rarea`in' = .; g norarea`in' = .; g meanarea`in' = .; 
	g overlap`in' = .;
	vallist sessub;
	foreach s in `r(list)' {;
		preserve;
		keep if(ingroup==`in'&sessub==`s');
		vallist extv; 
		foreach i in `r(list)' {; 
			vallist c1 if(extv==`i'); 
			foreach j in `r(list)' {; 
				sum p if(extv==`i'&c1==`j'); 
				replace retention = 0 if(extv==`i'&c1==`j'&r(mean)<=.5);
				replace retention = 1 if(extv==`i'&c1==`j'&r(mean)>.5); 
				replace p = r(mean) if(extv==`i'&c1==`j'); 
				};
			};
			local obs = 61*16; set obs `obs'; egen extvval = seq(), from(5) to(65); 
			bysort extvval: egen c1val = seq(), from(5) to(20);  
			forval r = 0/1 {;
				if(`r'==0){; 
					local v "nor"; local sign1 "<="; local sign2 ">="; 
				};
				else {; 
					local v "r"; local sign1 ">="; local sign2 "<="; 
				};
				g `v'extvindex = .; g `v'index = 0; local i = 1; 
				gsort -extv; vallist extv if(retention==`r');  
				foreach extv in `r(list)' {;
					g `v'c1index`i' = .;
					vallist c1 if(extv==`extv'&retention==`r');
					g `v'extv`i' = `extv'; local j = 1;
					foreach c1 in `r(list)' {; 
						g `v'c1`i'_`j' = `c1'; local j = `j'+1;
					};
					replace `v'c1index`i' = `j'-1; local i = `i'+1;
				};
				replace `v'extvindex = `i'-1; sum `v'extvindex; 
				local max = r(max); 
				forval i = 1/`max' {;
					sum `v'c1index`i'; 
					forval j = 1/`r(max)' {; 
						if(`r'==0){; 
							local extvmax = 65; local c1max = 20; 
						};
						else {; 
							local extvmax = 0; local c1max = 0; 
						};
						sum `v'extv`i'; sum `v'c1`i'_`j'; sum extvval; 
						replace `v'index = 1 
						if(extvval`sign1'`v'extv`i'&c1val`sign1'`v'c1`i'_`j'&
						(`extvmax'`sign2'`v'extv`i'|`c1max'`sign2'`v'c1`i'_`j'));
						sum extvval if(`v'index==1); local extvmax = r(max); 
						sum c1val if(`v'index==1); local c1max = r(max);
					};
				};
			};	
			count if(rindex==1); local rarea = r(N); 
			count if(norindex==1); local norarea = r(N);
			count if(rindex==1&norindex==1); local overlap = r(N);
			restore; 
			replace rarea`in' = `rarea' if(sessub==`s'); 
			replace norarea`in' = `norarea' if(sessub==`s'); 	
			replace overlap`in' = `overlap' if(sessub==`s');
		};
	}; 
g area = 61*16; 
replace meanarea1 = (area - (norarea1+rarea1))/2 if(overlap1==0);
replace meanarea0 = (area - (norarea0+rarea0))/2 if(overlap0==0);
replace meanarea1 = (area - (norarea1+rarea1) + overlap1)/2 if(overlap1~=0);
replace meanarea0 = (area - (norarea0+rarea0) + overlap0)/2 if(overlap0~=0);
g area1 = area-(norarea1+meanarea1);
g area0 = area-(norarea0+meanarea0);
g diffarea = area1 - area0;

* Compare bias measure to bias measure generated from baseline electiondecision;
preserve; 
use `data'/LandaDuell_data_bias, clear; 
vallist sessub; local list `r(list)';
foreach s in `list' {; 
	sum diffarea if(sessub==`s'); local diff`s' = r(mean); 
	}; 
restore;	

g diffid = .;
vallist sessub; 
foreach s in `list' {; 
	replace diffid = `diff`s'' if(sessub==`s');
	};

collapse diffid diffarea, by(sessub);
stack diffarea diffid, i(diff); 
rename _stack baseline; recode baseline (2=0);
replace diff = abs(diff);

* Generate qoi; 
g variable = diff; g class_variable = baseline; g included = 1;
bs diff = r(diff), reps(100) seed(01010) l(90): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable, by(class_variable); local z = r(z);
fprank variable, by(class_variable);
ttest variable, by(class_variable);
di "-------------------------------------------------";
di "Difference in D_i by treatment: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "Wilcoxon-test z-statistic: `z'";
di "-------------------------------------------------";

bs var = r(Var), reps(100) seed(01010) l(90) saving(var_diff_baseline, replace): 
	sum diff if(baseline==1), d;
bs var = r(Var), reps(100) seed(01010) l(90) saving(var_diff_id, replace): 
	sum diff if(baseline==0), d;
preserve; use var_diff_baseline, clear; g g = 1; append using var_diff_id;
replace g = 0 if(g==.); ttest var, by(g); restore;
permute diff z = r(z), reps(100): ranksum diff, by(baseline);

erase var_diff_baseline.dta;
erase var_diff_id.dta;

*=============================================================================;
* RUNS FOR A LONG TIME, COMMENT OUT IF YOU STILL WANT TO RUN!
* Randomization test: generate bias measure based on difference in areas 
* Load data; 
*use `data'/LandaDuell_data, clear; 
*keep if(treatment==identity&period>0&playerid==voter);
*
******************************************************************************;
* Generate qoi;
*include `run_files'/prg_sim_bias;
*use `data'/sim_bias, clear;
*
*preserve; 
*use `data'/LandaDuell_data_bias, clear; 
*unique sessub if(overlap0==0); local prop0old = `r(sum)'/70;
*unique sessub if(overlap1==0); local prop1old = `r(sum)'/70; 
*restore;
*
*sum prop1; 
*di "Critical value for in-matches:" (`prop1old'-r(mean))/r(sd);
*di 1-normal((`prop1old'-r(mean))/r(sd));
*
*sum prop0;
*di "Critical value for out-matches:" (`prop0old'-r(mean))/r(sd);
*di 1-normal((`prop0old'-r(mean))/r(sd));

*==================================================================================================;
* Figure E.2: Difference in predicted probability of retention in in- vs out-group matches for 
* smoothed (type_omega, choice 1)-pairs
*==================================================================================================;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

******************************************************************************;
* Generate qoi;	
forval i = 2/10 {;
	g round`i' = 0; replace round`i' = 1 if(round==`i');
	};
g inbias = 0; replace inbias = 1 if(bias==1);
g p = .; g meanp1 = .; g meanp0 =.; g N = .; g N1 = .; g N0 = .; g fd = .; 
replace absbias = 0 if(absbias>.1); replace absbias = 1 if(absbias<=.1);

forval i = 0/1 {;
	forval j = 5/20 {; 
		forval k = 5/65 {; 
			forval l = 0/1 {;
				sum election if(c1>=`j'-1&c1<=`j'+1&extv>=`k'-4&extv<=`k'+4&ingroup==`l'&inbias==`i');
				replace meanp`l' = r(mean) if(c1==`j'&extv==`k'&inbias==`i'); 
				replace N`l' = r(N) if(c1==`j'&extv==`k'&inbias==`i');
				};
			replace fd = meanp1 - meanp0 if(c1==`j'&extv==`k'&inbias==`i'); 
			replace N = N1 + N0 if(c1==`j'&extv==`k'&inbias==`i'); 
			}; 
		}; 
	};

******************************************************************************;
* Graph qoi;
forval i = 0/1 {;
	if(`i'==0){; 
		local save "out-bias"; local sc "ysc(alt)"; local ti "Out"; 
	}; 
	else {; 
		local save "in-bias"; local sc ""; local ti "In"; 
	};
	preserve; keep if(inbias==`i'); local set "`sc' yti("") xti("")";
	include `programs'/prg_graph_heat_fd.do; restore;   
	gr combine plot0, `graphr' `grcom' rows(1);
	gr export `output'/heat_fd_`save'.eps, replace;
	gr export `output'/heat_fd_`save'.pdf, replace; 
	};

*=================================================================================================;
* Figure E.3 ROC-curve of electiondecision over type_omega x choice 1 by bias
*=================================================================================================;
use `data'/LandaDuell_data, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==voter); 

******************************************************************************;
* Recode;
preserve; 
use `data'/LandaDuell_data_bias, clear; 
vallist sessub; local list `r(list)';
foreach s in `list' {; 
	sum bias if(sessub==`s'); local bias`s' = r(mean); 
	sum diffarea if(sessub==`s'); local diffarea`s' = r(mean); 
	sum absbias if(sessub==`s'); local absbias`s' = r(mean); 
	}; 
restore;	

vallist sessub if(treatment==identity);
g bias = .; g diffarea = .; g absbias = .; vallist sessub; 
foreach s in `list' {; 
	replace bias = `bias`s'' if(sessub==`s'); 
	replace diffarea = `diffarea`s'' if(sessub==`s');
	replace absbias = `absbias`s'' if(sessub==`s'); 
	};
keep if(((ingroup==1&bias==1)|(ingroup==0&bias==-1))|treatment==baseline);

******************************************************************************;
g variable = extv*c1; g inbias = 1 if(bias==1); replace inbias = 0 if(bias==-1);
replace inbias = 2 if(bias==.);
bs qoi = r(diff), reps(100) l(90) seed(01010): diffroc_bias; 
mat ci = e(ci_percentile); mat qoi = e(b); local lb = ci[1,1]; local ub = ci[2,1]; 
di "-------------------------------------------------";
di "Difference in area under roc curve (electiondecision vs extv*c1) between in- and out-group biased";
di "voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

roccomp electiondecision variable, g by(inbias) `graphr' `set' leg(off)
plot1opts(lc(black) lp(solid) lw(thick) mlc(black) mfc(white))
plot2opts(lc(black) lp(solid) lw(thick) mc(black)) 
plot3opts(lc(gray) lp(solid) lw(thick) mc(gray))
rlopts(lc(black) lw(thick) lp(dash)) yti("") xti("");  
gr export `output'/roc_extvc1_bybias.pdf, replace;
gr export `output'/roc_extvc1_bybias.eps, replace;

*=================================================================================================;
* Figure E.4: Predicted probability of retention for smoothed (type_omega,choice 1)-pairs over bias
*=================================================================================================;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

******************************************************************************;
* Generate qoi;
g inbias = 0; replace inbias = 1 if(bias==1);
forval i = 2/10 {;
	g round`i' = 0; replace round`i' = 1 if(round==`i');
	};
g p = .; g meanp0 = .; g meanp1 = .; g N0 = .; g N1 = .; g N = .;
 
replace absbias = 0 if(abs(diffarea/area)>.1); 
replace absbias = 1 if(abs(diffarea/area)<=.1);
probit election c.c1 c.extv i.ingroup i.inbias c.extv#c.c1 c.extv#i.ingroup 
c.extv#i.inbias c.c1#i.ingroup c.c1#i.inbias i.ingroup#i.inbias c.extv#c.c1#i.ingroup 
c.extv#c.c1#i.inbias c.extv#i.ingroup#i.inbias c.c1#i.ingroup#i.inbias round 
absbias; 
forval i = 0/1 {;
	if(`i'==0){; 
		local save "out-bias"; local sc "ysc(alt)"; local ti "Out"; 
		}; 
		else {; 
			local save "in-bias"; local sc ""; local ti "In"; 
		};
	forval j = 0/1 {; 
		predictnl phat = normal(_b[_cons] + _b[c.c1]*c1 + _b[c.extv]*extv +
			_b[`j'.ingroup]*`j' + _b[`i'.inbias]*`i' + _b[c.extv#c.c1]*extv*c1 +
			_b[c.extv#`j'.ingroup]*extv*`j' + _b[c.extv#`i'.inbias]*extv*`i' +
			_b[c.c1#`j'.ingroup]*c1*`j' + _b[c.c1#`i'.inbias]*c1*`i' +
			_b[`j'.ingroup#`i'.inbias]*`j'*`i' + _b[c.extv#c.c1#`j'.ingroup]*extv*`j' + 
			_b[c.extv#c.c1#`i'.inbias]*extv*c1*`i' + 
			_b[c.extv#`j'.ingroup#`i'.inbias]*extv*`j'*`i' + 
			_b[c.c1#`j'.ingroup#`i'.inbias]*c1*`j'*`i' + _b[round]*round + 
			_b[absbias]*absbias) if e(sample); 
		replace p = phat if(ingroup==`j'&inbias==`i'); drop phat; 
		};
	forval c1 = 5/20 {; 
		forval tv = 5/65 {; 
			forval j = 0/1 {;
				sum p if(c1>=`c1'-1&c1<=`c1'+1&extv>=`tv'-4&extv<=`tv'+4&
				ingroup==`j'&inbias==`i');
				replace meanp`j' = r(mean) if(c1==`c1'&extv==`tv'&inbias==`i'); 
				replace N`j' = r(N) if(c1==`c1'&extv==`tv'&inbias==`i'); 
				}; 
			};
		}; 

******************************************************************************;
* Graph qoi;
	forval j = 0/1 {; 
		preserve; keep if(ingroup==`j'&inbias==`i');
		if(`j'==1){; 
			local set "`sc' xsc(alt)"; 
			}; 
		else if(`j'==0){; 
			local set "`sc'"; 
			};
		replace p = meanp`j'; replace N = N`j';
		include `programs'/prg_graph_heat_pp.do; drop pcat zero; 
		restore; 
		gr combine plot`j', `graphr'; 
		gr export `output'/heat_pp_`save'_t`j'.eps, replace;
		gr export `output'/heat_pp_`save'_t`j'.pdf, replace; 
		};  
	}; 

*=============================================================================;
* Comparison of difference of average retention rate in high type_omega, high effort-quadrant 
* and low type + noise, low effort-quadrant across biases; 
* Load data; 
use `data'/LandaDuell_data_bias, clear; 
keep if((bias==1&ingroup==1)|(bias==-1&ingroup==0));

******************************************************************************;
* qoi;
g hh = .; 
replace hh = 0 if(extv<35&c1<13);
replace hh = 1 if(extv>=35&c1>=13);

preserve;
g variable = election; g class_variable = hh; g included = 1 if(bias==1);
bs diff = r(diff), reps(100) seed(01010) l(90): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable, by(class_variable); local z = r(z);
fprank variable, by(class_variable);
ttest variable, by(class_variable);
di "-------------------------------------------------";
di "Difference in electiondecision by quadrant in (type_omega,choice 1)-space for in-group";
di "bias voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = election; g class_variable = hh; g included = 1 if(bias==-1);
bs diff = r(diff), reps(100) seed(01010) l(90): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable, by(class_variable); local z = r(z);
fprank variable, by(class_variable);
ttest variable, by(class_variable);
di "-------------------------------------------------";
di "Difference in electiondecision by quadrant in (type_omega,choice 1)-space for in-group";
di "bias voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

*==================================================================================================;
* Excluding low bias subjects (30 subjects excluded);
* Test of difference in area under the ROC-curve (electiondecision) over type_omega, effort, and 
* type_omega x effort
*=============================================================================;
* Load data;
use `data'/LandaDuell_data_bias, clear; 
keep if((ingroup==1&bias==1)|(ingroup==0&bias==-1));
keep if(abs(diffarea/area)>.1);

******************************************************************************;
* Generate qoi;
g variable = extv*c1;
bs qoi = r(diff), reps(100) l(90) seed(01010) cl(sessub absbias): diffroc_bias; 
mat ci = e(ci_percentile); mat qoi = e(b); local lb = ci[1,1]; local ub = ci[2,1]; 
di "-------------------------------------------------";
di "Difference in area under roc curve (electiondecision vs extv*c1): " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

*=============================================================================;
* Comparison of difference of average retention rate in (high type_omega, high effort)-quadrant 
* and low type + noise, low effort-quadrant across biases
*=============================================================================;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 
keep if((bias==1&ingroup==1)|(bias==-1&ingroup==0));
keep if(abs(diffarea/area)>.1);

******************************************************************************;
* Generate qoi;
g hh = .; 
replace hh = 0 if(extv<35&c1<13);
replace hh = 1 if(extv>=35&c1>=13);

preserve;
g variable = election; g class_variable = hh; g included = 1 if(bias==1);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(session absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable if(included==1), by(class_variable); 
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in electiondecision by quadrant in (type_omega,choice 1)-space for in-group";
di "biased voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = election; g class_variable = hh; g included = 1 if(bias==-1);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(session absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable if(included==1), by(class_variable); 
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in electiondecision by quadrant in (type_omega,choice 1)-space for in-group";
di "biased voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

*=============================================================================;
* Comparison of summary stats of choice 2 for in- and out-biased voters across matches
* Load data; 
use `data'/LandaDuell_data_bias, clear; 
keep if(abs(diffarea/area)>.1);

******************************************************************************;
* Generate qoi;
preserve;
g variable = c2; g class_variable = ingroup; g included = 1 if(bias==1&c2>0);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(session absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable if(included==1), by(class_variable); 
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in choice 2 between in- and out-group matches for in-group biased voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

preserve;
g variable = c2; g class_variable = ingroup; g included = 1 if(bias==-1&c2>0);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(session absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable if(included==1), by(class_variable); 
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in choice 2 between in- and out-group matches for out-group biased voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";
restore;

*=============================================================================;
* In-bias a function of favorable retention choices experienced as low type in in-matches?
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

vallist sessub; local list "`r(list)'";
foreach s in `list' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	sum diffarea if(sessub==`s'); local diff`s' = r(mean);
	sum absbias if(sessub==`s'); local absbias`s' = r(mean);
	};

use `data'/LandaDuell_data, clear; 
keep if(treatment==identity&period>0&playerid==representative&firsthalf==1);

g bias = .; g diffarea = .; g absbias = .;
foreach s in `list' {;
	replace bias = `bias`s'' if(sessub==`s'-10000);
	replace diffarea = `diff`s'' if(sessub==`s'-10000);
	replace absbias = `absbias`s'' if(sessub==`s'-10000); 

	};

keep if(abs(diffarea/`area')>.1);

******************************************************************************;
* Recode;
g favorable = .; g prop_favorable = .;
replace favorable = 1 if(election==1&ingroup==1&lowtype==1);
bysort sessub: egen total_favorable = count(favorable);

vallist sessub;
foreach s in `r(list)' {;
	count if(ingroup==1&lowtype==1&sessub==`s'); 
	replace prop_favorable = total_favorable/r(N) if(sessub==`s');
	};	

******************************************************************************;
* Generate qoi;
g class_favorable = 0 if(prop_favorable<.5); 
replace class_favorable = 1 if(prop_favorable>=.5);
g variable = diffarea/((65-5)*(20-5)); g class_variable = class_favorable; 
g included = 1 if(bias==1&prop_favorable~=.);
bs diff = r(diff), reps(100) seed(01010) l(90) cl(session absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable if(included==1), by(class_variable); 
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in D_i of representatives in the first half who experienced favorable treatment";
di "in in-group matches as low type: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

*=============================================================================;
* Difference in means and difference in distribution of choice 2 by biases
*=============================================================================;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	sum diffarea if(sessub==`s'); local diffarea`s' = r(mean);
	sum absbias if(sessub==`s'); local absbias`s' = r(mean);
	};

use `data'/LandaDuell_data, clear; 
keep if(treatment==identity&period>0&playerid==representative);

g bias = .; g diffarea = .; g absbias = .; vallist sessub; 
foreach s in `r(list)' {;
	local r = `s' + 10000;
	replace bias = `bias`r'' if(sessub==`s');
	replace diffarea = `diffarea`r'' if(sessub==`s');
	replace absbias = `absbias`r'' if(sessub==`s');
	};

keep if(abs(diffarea/`area')>.1);

******************************************************************************;
* Generate qoi;
g variable = c2; g class_variable = 0; replace class_variable = 1 if(bias==1); 
g included = 1 if(c2>0); 
bs diff = r(diff), reps(100) seed(01010) l(90) cl(session absbias): diff_means;
mat ci = e(ci_percentile); local lb = ci[1,1]; local ub = ci[2,1]; 
mat qoi = e(b); local qoi = qoi[1,1]; 
ranksum variable if(included==1), by(class_variable); 
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);
di "-------------------------------------------------";
di "Difference in choice 2 between in-group biased and out-group biased voters: " qoi[1,1];
di	"Lower bound: " `lb';
di	"Upper bound: " `ub'; 
di "-------------------------------------------------";

replace included = .; replace included = 1 if(c2>0&lowtype==1); 
bs diff = r(diff) if(included==1), reps(1000) seed(01010) l(90) cl(sessub): 
diff_means; 
ranksum variable if(included==1), by(class_variable);
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);

replace included = .; replace included = 1 if(c2>0&lowtype==0); 
bs diff = r(diff) if(included==1), reps(1000) seed(01010) l(90) cl(sessub): 
diff_means; 
ranksum variable if(included==1), by(class_variable);
fprank variable if(included==1), by(class_variable);
ttest variable if(included==1), by(class_variable);

*==================================================================================================;
* H. Exit-survey
*==================================================================================================;
use	`data'/LandaDuell_exitSurvey, clear;
tab typeoreffort if(treatment==1);
tab typeoreffort if(treatment==2);
tab thresholdconsequence if(treatment==1);
tab thresholdconsequence if(treatment==2);	
	
*==================================================================================================;
*==================================================================================================;
* III. Exit
*==================================================================================================;
exit;
*==================================================================================================;
*==================================================================================================;
*==================================================================================================;

